package com.module.lemlin.owner

import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.app.OwnerStatusBar

abstract class OwnerAbstractFragment : Fragment(), View.OnTouchListener {

    private var isFirstLoad = false

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        view.setOnTouchListener(this)
        doBeforeInit()
        initViewMode()
        initView()
    }

    override fun onResume() {
        super.onResume()
        if (!isFirstLoad) {
            initData()
            isFirstLoad = true
        } else {
            onRefreshLoad()
        }
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        return true
    }

    /**
     * 状态栏设置
     * */
    private fun setStatusBar(statusBar: OwnerStatusBar = OwnerStatusBar()) {
        BarUtils.setStatusBarLightMode(
            requireActivity().window, statusBar.lightMode
        )
        BarUtils.setStatusBarColor(
            requireActivity().window, ColorUtils.getColor(statusBar.barColor)
        )
        BarUtils.setNavBarColor(
            requireActivity().window, ColorUtils.getColor(statusBar.navColor)
        )
        statusBar.addView?.let {
            BarUtils.addMarginTopEqualStatusBarHeight(it)
        }
    }

    /**
     * 请求权限结果
     */
    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String>, grantResults: IntArray,
    ) {
        var granted = true
        for (result in grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                granted = false
            }
        }
        if (!handlePermissionResult(requestCode, granted)) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
    }

    /**
     * 检查是否已被授权危险权限
     */
    private fun checkDangerousPermissions(permissions: Array<String>): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true
        }
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(requireContext(),
                    permission) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), permission)
            ) {
                return false
            }
        }
        return true
    }

    /**
     * 请求权限
     */
    open fun requestDangerousPermissions(permissions: Array<String>, requestCode: Int) {
        if (checkDangerousPermissions(permissions)) {
            handlePermissionResult(requestCode = requestCode, granted = true)
            return
        }
        ActivityCompat.requestPermissions(requireActivity(), permissions, requestCode)
    }

    /**
     * 处理请求危险权限的结果
     */
    open fun handlePermissionResult(requestCode: Int, granted: Boolean): Boolean {
        return false
    }

    /**
     * 初始化视图和数据之前进行操作
     */
    open fun doBeforeInit() = Unit

    /**
     * 初始化状态栏
     * */
    open fun initStatusBar(statusBar: OwnerStatusBar = OwnerStatusBar()) =
        setStatusBar(statusBar)

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

    /**
     * 初始化视图
     */
    open fun initView() = Unit

    /**
     * 初始化数据
     */
    open fun initData() = Unit

    /**
     * 刷新数据
     */
    open fun onRefreshLoad() = Unit

    /**
     * toast提示
     * */
    fun toast(message: String?) {
        ToastUtils.showShort(message)
    }

    /**
     * debug logger
     * */
    fun debugLog(any: Any) {
        LogUtils.e(javaClass.name, any)
    }
}